package com.societyGames.flashForms
{
	import flash.display.DisplayObject;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;

	public class TabItemsContainer extends Sprite
	{
		private var _desiredHeight:Number;
		private var _desiredWidth:Number;
		private var _tabItems:Array = []; //Tab
		private var _selectedTabItem:TabItem;
		private var _headersContainer:HeadersContainer;
		
		public function TabItemsContainer(tabLine:DisplayObject)
		{
			this._headersContainer = new HeadersContainer(tabLine);
			this.addChild(this._headersContainer);
		}
		
		public override function set width(value:Number):void
		{	
			if (value != this._desiredWidth)
			{
				this._desiredWidth = value;
				recalculateWidth();
			}
		}

		private function recalculateWidth():void
		{
			this._headersContainer.width = _desiredWidth;
			if (_selectedTabItem != null)
			{	
				this._selectedTabItem.content.width = this._desiredWidth;
			}
		}
		
		public override function set height(value:Number):void
		{
			if (value != this._desiredHeight)
			{
				this._desiredHeight = value;
				recalculateHeight();
			}
		}
		
		private function recalculateHeight():void
		{	
			var availableContentHeight:Number = this._desiredHeight - this._headersContainer.headerHeight; 
			//Shrink the content or remove it altogether.
			if (this._selectedTabItem != null)
			{
				if (availableContentHeight > 0)
				{
					this._selectedTabItem.content.visible = true;
					this._selectedTabItem.content.height = availableContentHeight;
					this._selectedTabItem.content.y = this._headersContainer.headerHeight;
				}
				else
				{
					this._selectedTabItem.content.visible = false;
					this._selectedTabItem.content.height = 0;
				}
			}
			//If there's enough room, let the header take up as much as it wants.
			if (availableContentHeight >= 0)
			{
				this._headersContainer.height = this._headersContainer.headerHeight;
			}
			else
			{
				this._headersContainer.height = this._desiredHeight;
			}
		}
		
		public function addTabItem(tabItem:TabItem):void
		{	
			//Keep in main list.
			this._tabItems.push(tabItem);
			
			//Add to render list and listen for click.
			this._headersContainer.addHeader(tabItem.tabHeader);
			this.addChild(tabItem.content);
			tabItem.headerClickedEventObject.addEventListener(tabHeaderClickedHandler);
			
			//Prep the tab item content
			tabItem.content.visible = false;
			tabItem.content.x = 0;
			
			//Select if there isn't one.
			if (this._selectedTabItem == null)
			{
				selectTabItem(tabItem);	
			}
			else
			{
				recalculateHeight();				
			}
		}

		public function get tabItemCount():int
		{
			return this._tabItems.length;
		}
		
		public function getTabItem(index:int):TabItem
		{
			return TabItem(this._tabItems[index]);
		}
		
		public function removeTabItem(tabItem:TabItem):void
		{
			//See if the tab is present.	
			var index:int = this._tabItems.indexOf(tabItem);
			if (index != -1)
			{
				//remove from main list.
				this._tabItems.splice(index, 1);
				
				//Stop rendering
				this._headersContainer.removeHeader(tabItem.tabHeader);
				this.removeChild(tabItem.content);
				
				//Don't respond to clicks anymore
				tabItem.headerClickedEventObject.removeEventListener(tabHeaderClickedHandler);
				
				//Choose the first tab if we were selected.
				if (this._selectedTabItem == tabItem)
				{
					if (this._tabItems.length > 0)
					{
						selectTabItem(this._tabItems[0]);
					}
					else //Just clear it to nothing.
					{
						selectTabItem(null);
					}
				}
			}
		}
		
		public function selectTabItem(tab:TabItem):void
		{
			if (this._selectedTabItem != tab)
			{
				if (this._selectedTabItem != null)
				{
					this._selectedTabItem.content.visible = false;
				}				
				if (tab != null)
				{
					//Move us to the front. We better be in the render list.
					this._headersContainer.selectHeader(tab.tabHeader);
					this.removeChild(tab.content);
					this.addChild(tab.content);
					tab.content.visible = true;
					tab.content.x = 0;
				}
				this._selectedTabItem = tab;
				
				//Rekick the sizing
				recalculateHeight();
				recalculateWidth();
			}
		}
		
		private function tabHeaderClickedHandler(event:Event):void
		{	
			selectTabItem(TabItem(event.target));
		}
	}
}